package org.ands.client.ala;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ands.apps.PropertyHelper;
import org.ands.crosswalk.CrossWalkEntry;
import org.ands.crosswalk.CrossWalkEntryRetrieverCSV;
import org.ands.data.Contents;
import org.ands.data.ContentsHelper;
import org.ands.data.IClientContentsRetriever;
import org.ands.json.JsonHelper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

// TODO: Auto-generated Javadoc
/**
 * The Class ContentsRetrieverALASpatial.
 */
public class ContentsRetrieverALASpatial implements IClientContentsRetriever {
	
	/**
	 * Constructs a list of XMLContents objects from the xpaths-and-value pairs
	 * that it determines from Atlas of Living Australia json.
	 * The list contains many XMLContents objects that represent many DataResource
	 * objects and many related DataProvider objects that are associated with
	 * each DataResource.
	 *
	 * @param propertyHelper the property helper
	 * @return          			If successful, a list of XMLContents objects
	 * constructed from the values from ALA;
	 * <code>null</code> otherwise.
	 * @throws Exception the exception
	 * @see   	Contents
	 * @since     1.0
	 */
	@Override
	public Map<Integer, List<Contents>> getXmlContentsFromClient(final PropertyHelper propertyHelper) 
					throws Exception{
		
		final String uriAllSpatial = propertyHelper.getProperty("SPATIAL_URI", true);
		if((uriAllSpatial == null) || (uriAllSpatial.isEmpty())){
			throw new Exception("Requires valid uri");
		}
		
		final String collectionCrossWalkFile = propertyHelper.getProperty("SPATIAL_COLLECTION_CROSSWALK", true);
		if((collectionCrossWalkFile == null) || (collectionCrossWalkFile.isEmpty())){
			throw new Exception("Requires valid collection crosswalk filename");
		}
		
		String jsonAllSpatialLayers = JsonHelper.contentFromUri(uriAllSpatial);
		assert(jsonAllSpatialLayers != null);
		assert(jsonAllSpatialLayers.length() > 0);
		
		CrossWalkEntryRetrieverCSV crossWalkEntryRetrieverCSV = new CrossWalkEntryRetrieverCSV();
		// Map where int is the index to the registryObject (starting at one) and the list of XmlContents
		// is all of the XmlContent objects per registryObject at the index
		Map<Integer, List<Contents> > xmlContentsAllRegistryObjectsMap = new HashMap<Integer, List<Contents> >();
		
		ArrayNode spatialLayerNodeArray = JsonHelper.arrayNodeFromString(jsonAllSpatialLayers);
		Integer collectionCount = 1; // Accumulate count of DataResources // Used for index on RegistryObject
		ArrayList<CrossWalkEntry> collectionCrossWalkEntryList = crossWalkEntryRetrieverCSV.getCrossWalkEntryListFromFile(collectionCrossWalkFile);
		assert((collectionCrossWalkEntryList != null) && (!collectionCrossWalkEntryList.isEmpty()));
		
		for(JsonNode spatialLayerMetadataNode: spatialLayerNodeArray){
			JsonHelper spatialLayerMetadataHelper = new JsonHelper(spatialLayerMetadataNode);
			
			List<String> idList = spatialLayerMetadataHelper.contentListFromPath("id");
			if(idList.size() != 1){
				System.err.println("No id retrievable");
				continue;
			}
			
			String id = idList.get(0);
			System.out.println("Id for spatial layer["+id+"].");
			
			try{
				ArrayList<Contents> collectionList = ContentsHelper.xmlContentsFromTree(spatialLayerMetadataHelper, collectionCrossWalkEntryList, collectionCount);
				
				assert(collectionList != null);
				assert(!collectionList.isEmpty());
			
				xmlContentsAllRegistryObjectsMap.put(collectionCount, collectionList);
				collectionCount++;
				
			}catch(Exception e){
				System.err.println("Failed to obtain required spatial layer ["+id+"] - Exception: ["+e.getMessage()+"].\n");
				e.printStackTrace();
				//assert(false);
			}
		}
		
		if(xmlContentsAllRegistryObjectsMap.size() <= 0){
				throw new Exception("Unable to create XMLContents from uri ["+uriAllSpatial+"].");
		}
		
		return xmlContentsAllRegistryObjectsMap;
		
	}
	
	
	/* (non-Javadoc)
	 * @see org.ands.data.IContentsRetriever#getOutFileFromConfig(org.ands.apps.PropertyHelper)
	 */
	@Override
	public String getOutFileFromConfig(final PropertyHelper propertyHelper)
			throws Exception {
		
		return propertyHelper.getProperty("RIF_CS_XML_OUT_ALA_SPATIAL", true);
	}
		
}
